package com.itheima.resp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;

@WebServlet("/listStudentServlet")
public class ListStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader br = new BufferedReader(new FileReader("d:\\stu.txt"));
        ArrayList<Student> list = new ArrayList<>();
        String line;
        while ((line = br.readLine()) != null) {
            Student stu = new Student();
            String[] arr = line.split(",");
            stu.setName(arr[0]);
            stu.setAge(Integer.parseInt(arr[1]));
            stu.setScore(Integer.parseInt(arr[2]));
            list.add(stu);
        }
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter pw = resp.getWriter();
        for (Student student : list) {
//            pw.write(student.getName() + "," + student.getAge() + "," + student.getScore());
//            pw.write("<hr>");
            pw.write("<table border='1' align='center'  width=50% >" +
                    "<tr>" + "<td>" + student.getName() + "</td>" + "<td>" + student.getAge() + "</td>" + "<td>" + student.getScore() + "</td>" + "</tr>" + "</table>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
